import subprocess

def test1():
    proc = subprocess.Popen(['python'])
    try:
        outs, errs = proc.communicate(input="print('sb')", timeout=2)
    except TimeoutExpired:
        proc.kill()
        outs, errs = proc.communicate()


## ==========================================================================================================================

def test2():
    # 创建子进程，将标准输入、标准输出和标准错误与管道连接起来
    process = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    # 向子进程的标准输入写入数据
    input_data = "print('fuck you')\nimport os\nprint(os.getenv('TMP'))"
    process.stdin.write(input_data.encode('utf-8'))
    process.stdin.close()

    # 从子进程的标准输出和标准错误读取数据
    output_data = process.stdout.read()
    error_data = process.stderr.read()

    # 等待子进程结束
    process.wait(timeout=2)

    # 打印输出和错误信息
    print("Output:", output_data.decode())
    print("Error:", error_data.decode())


## =========================================================================================================================

def test3():
    process = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    pre = ['import math\n']
    for item in pre:
        process.stdin.write(item.encode('utf-8'))
    while True:
        input_data = input(">>>")
        process.stdin.write(input_data.encode('utf-8'))
        output_data = process.stdout.read()
        error_data = process.stderr.read()
        print(output_data.decode('utf-8'))
        print(error_data.decode('utf-8'))
    process.stdin.close()

def xiecheng_test():
    #-*- coding:utf8 -*-
    import asyncio

    async def test(i):
        print('test_1', i)
        await asyncio.sleep(1)
        print('test_2', i)

    loop = asyncio.get_event_loop()
    tasks = [test(i) for i in range(3)]
    loop.run_until_complete(asyncio.wait(tasks))
    loop.close()


import asyncio

async def my_coroutine(p):
    print(f"Coroutine{p} is running")
    await asyncio.sleep(5)
    print(f"Coroutine{p} is done")
    return p

async def main():
    loop = asyncio.get_running_loop()
    print(loop)
    await my_coroutine(1)
    await my_coroutine(2)
    await my_coroutine(3)
    # t3 = asyncio.create_task(my_coroutine(3))
    # asyncio.create_task(my_coroutine(1))
    # asyncio.create_task(my_coroutine(2))
    # import time
    # time.sleep(3)
    # await t3
    print('main函数结束')

if __name__ == '__main__':
    asyncio.run(main())